package com.game.fighting.facaory;

import com.almasb.fxgl.dsl.FXGL;
import com.almasb.fxgl.dsl.components.ExpireCleanComponent;
import com.almasb.fxgl.dsl.components.OffscreenCleanComponent;
import com.almasb.fxgl.dsl.components.ProjectileComponent;
import com.almasb.fxgl.entity.Entity;
import com.almasb.fxgl.entity.EntityFactory;
import com.almasb.fxgl.entity.SpawnData;
import com.almasb.fxgl.entity.Spawns;
import com.almasb.fxgl.texture.AnimatedTexture;
import com.almasb.fxgl.texture.AnimationChannel;
import com.game.fighting.config.ArcheryType;
import com.game.fighting.component.*;
import javafx.geometry.Point2D;
import javafx.util.Duration;

/**
 * @author LeeWyatt
 */
public class ArcheryEntityFactory implements EntityFactory {

    private Duration seconds;

    @Spawns("bg")
    public Entity newBg(SpawnData data) {
        return FXGL.entityBuilder(data)
                .view("bg.png")
                .neverUpdated()
                .build();
    }

    @Spawns("bow")
    public Entity newBow(SpawnData data) {
        return FXGL.entityBuilder(data)
                .with(new BowComponent())
                .build();
    }

    private static final Point2D MOVE_TO_RIGHT = new Point2D(1, 0);

    @Spawns("arrow")
    public Entity newArrow(SpawnData data) {
        return FXGL.entityBuilder(data)
                .type(ArcheryType.ARROW)
                .with(new ArrowComponent())
                .with(new ProjectileComponent(MOVE_TO_RIGHT, 600))
                .with(new OffscreenCleanComponent())
                .collidable()
                .build();
    }

    @Spawns("target")
    public Entity newTarget(SpawnData data) {
        return FXGL.entityBuilder(data)
                .type(ArcheryType.TARGET)
                .with(new TargetComponent())
                .collidable()
                .build();
    }

    @Spawns("star")
    public Entity newStar(SpawnData data) {
        Duration duration = Duration.seconds(0.3);
        return FXGL.entityBuilder(data)
                .view(
                        new AnimatedTexture(new AnimationChannel(
                                FXGL.image("star.png")
                                , duration, 8)).play()
                )
                .with(new ExpireCleanComponent(duration))
                .build();
    }


    @Spawns("score")
    public Entity newScore(SpawnData data) {
        return FXGL.entityBuilder(data)
                .with(new ScoreComponent())
                .with(new ExpireCleanComponent(Duration.seconds(0.8)))
                .build();
    }

    @Spawns("shootBar")
    public Entity newShootBar(SpawnData data) {
        return FXGL.entityBuilder(data)
                .type(ArcheryType.SHOOT_BAR)
                .with(new ShootBarComponent())
                .build();
    }


    @Spawns("click")
    public Entity newClick(SpawnData data) {
        seconds = Duration.seconds(0.2);
        return FXGL.entityBuilder(data)
                .view(
                        new AnimatedTexture(
                        new AnimationChannel(FXGL.image("click.png")
                        , seconds, 7)).play()
                )
                .with(new ExpireCleanComponent(seconds))
                .build();
    }

}


